<template>
	<view class="order-page">
		<view class="tab-box">
			<u-tabs
				:list="tabList"
				:current="tabCurrent"
				:scrollable="false"
				lineColor="#333333"
				:activeStyle="{ color: '#333', fontSize: '30rpx', fontWeight: 'bold' }"
				:inactiveStyle="{ color: '#333', fontSize: '30rpx' }"
				@change="tabChange"
			></u-tabs>
		</view>
		
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="goDetail(item.id)">
				<view class="top">
					<view class="number">订单号：{{ item.order_sn }}</view>
					<view class="status">
						<text v-if="item.status == 1">待发货</text>
						<text v-else-if="item.status == 4">待收货</text>
						<text v-else-if="item.status == 5">已收货</text>
					</view>
				</view>
				<view class="goods-box">
					<view class="goods-item" v-for="(itm, idx) in item.item" :key="idx">
						<image class="image" :src="itm.thumb" mode="aspectFill"></image>
						<view class="content">
							<view class="title u-line-1">{{ itm.goods_name }}</view>
							<view class="specs-box">
								<text class="specs">规格：一份</text>
								<text class="number">x{{ itm.num }}</text>
							</view>
							<view class="price">
								<text class="icon">￥</text>
								<text>{{ itm.amount }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- <view class="item" @click="goDetail(2)">
				<view class="top">
					<view class="number">订单号：20250103170000076</view>
					<view class="status">待支付</view>
				</view>
				<view class="goods-box">
					<view class="goods-item">
						<image class="image" src="https://cdn.uviewui.com/uview/album/1.jpg" mode="aspectFill"></image>
						<view class="content">
							<view class="title u-line-1">小哇AI成长笔记本</view>
							<view class="specs-box">
								<text class="specs">规格：一份</text>
								<text class="number">x1</text>
							</view>
							<view class="price">
								<text class="icon">￥</text>
								<text>364.74</text>
							</view>
						</view>
					</view>
				</view>
				<view class="btn-box">
					<button class="btn" @click.stop>立即支付</button>
				</view>
			</view> -->
			
			<u-loadmore :status="status" />
		</view>
	</view>
</template>

<script>
	import { getOrderList } from '@/api/api'

	export default {
		data () {
			return {
				tabCurrent: 0,
				tabList: [{
					name: '全部'
				}, {
					name: '待发货'
				}, {
					name: '待收货'
				}, {
					name: '已收货'
				}],
				
				list: [],
				/* 分页 */
				status: 'loadmore',
				page: 0,
				last_page: 1,
			}
		},
		onLoad (options) {
			if (options.type) {
				this.tabCurrent = Number(options.type)
			}

			this.loadData()
		},
		created() {
			uni.$on('onRefresh', () => {
				this.loadData()
			})
		},
		onReachBottom () {
			this.getListData()
		},
		methods: {
			loadData () {
				this.page = 0
				this.last_page = 1
				this.list = []
				this.getListData()
			},
			async getListData () {
				if(this.page >= this.last_page) return
			
				this.status = 'loading'
				this.page = ++this.page
				
				let status = 0
				if (this.tabCurrent == 1) {
					status = 1
				} else if (this.tabCurrent == 2) {
					status = 4
				} else if (this.tabCurrent == 3) {
					status = 5
				}
			
				const req = await getOrderList({
					page: this.page,
					page_size: 10,
					status: status
				})
				
				if (req) {
					this.list = this.list.concat(req.data.data)
					this.last_page = req.data.last_page
					
					if (this.page >= this.last_page) {
						this.status = 'nomore'
					} else {
						this.status = 'loading'
					}
					
					return ''
				}
			},
			
			tabChange (e) {
				this.tabCurrent = e.index
				this.loadData()
			},
			goDetail (id) {
				uni.navigateTo({
					url: '/pages/my/order/detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
.order-page {
	min-height: 100%;
	background-color: $bg-color;
	.tab-box {
		position: sticky;
		top: 0;
		left: 0;
		right: 0;
		z-index: 10;
		background-color: #fff;
	}
	.list {
		padding: 30rpx;
		.item {
			padding: 30rpx;
			border-radius: 16rpx;
			background-color: #fff;
			&:not(:last-child) {
				margin-bottom: 30rpx;
			}
			.top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				.number {
					color: #666;
					font-size: 26rpx;
				}
				.status {
					font-size: 28rpx;
				}
			}
			.goods-box {
				margin-top: 30rpx;
				.goods-item {
					display: flex;
					&:not(:last-child) {
						margin-bottom: 14rpx;
					}
				}
				.image {
					width: 140rpx;
					height: 140rpx;
					border-radius: 14rpx;
				}
				.content {
					margin-left: 20rpx;
					flex: 1;
					display: flex;
					flex-direction: column;
					.title {
						color: #333;
						font-size: 30rpx;
					}
					.specs-box {
						margin-top: auto;
						display: flex;
						align-items: center;
						justify-content: space-between;
						.specs {
							color: #888;
							font-size: 24rpx;
						}
						.number {
							color: #333;
							font-size: 24rpx;
						}
					}
					.price {
						margin-top: auto;
						color: #333;
						font-size: 28rpx;
						.icon {
							font-size: 16rpx;
						}
					}
				}
			}
			.btn-box {
				margin-top: 20rpx;
				display: flex;
				justify-content: flex-end;
				.btn {
					margin: 0;
					padding: 0;
					border-radius: 54rpx;
					width: 128rpx;
					height: 54rpx;
					color: #fff;
					font-size: 22rpx;
					line-height: 54rpx;
					background-color: #cd2a1b;
				}
			}
		}
	}
}
</style>
